package ti25_2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Room {
    private int num;
    private List<Student>  studentList = new ArrayList<>();


    public boolean check_in(Student student){
        // 判断宿舍剩余床位
        if (studentList.size()<6){
            // 判断学生性别
            if (studentList.size()!=0){
                if (studentList.get(0).isSex()==student.isSex()){
                    studentList.add(student);
                    System.out.println("分配宿舍成功");
                    return true;
                }else {
                    System.out.println("性别冲突");
                    return false;
                }
            } else {
                studentList.add(student);
                System.out.println("分配宿舍成功，添加第一位学生");
            }
        } else {
            System.out.println("宿舍没有空床位");
            return false;
        }
        return false;
    }

    public void show(){
//        【当前时间】，已分配 x 位学生，剩余 x 张空床
        Date  date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String nowTime = simpleDateFormat.format(date);
        int stu = studentList.size();
        int chuangWei = 6-stu;

        String content = nowTime+",已分配"+stu+"学生，剩余"+chuangWei+"张空位\n";
        content += "=====================================\n";
        content += String.format("%6s %4s %6s   %2s   %3s\n", "学号", "姓名", "班级", "性别", "年龄");
        content += "=====================================\n";

        for (Student student : studentList) {
            String sex = "男";
            if (!student.isSex()){
                sex = "女";
            }
            content += String.format("%6s %4s %6s %2s %3s\n", student.getNum(), student.getName(), student.getClasse(),
                    sex, student.getAge());
            content += "--------------------------------------\n";
        }
        System.out.println(content);

        FileOutputStream fos = null;
        try {
            byte[] bytes = content.getBytes();  // 将字符串转为字节数组
            fos = new FileOutputStream("C:\\Users\\azez1\\Desktop\\room_"+num+".txt");  // 获取文件对象
            fos.write(bytes); // 写入字节数组
            System.out.println("文件写入完成！");
        } catch (IOException e) {
            System.out.println("文件写入失败：" + e.getMessage());
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }


    public Room(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }
}
